package leetcode;

class Solution {
    /**
     * 面试题 08.01. 三步问题
     * https://leetcode.cn/problems/three-steps-problem-lcci/description/
     */
    public int waysToStep(int n) {
        if(n == 1) return 1;
        if(n == 2) return 2;
        if(n == 3) return 4;

        int[] dp = new int[n + 1];
        dp[1] = 1;
        dp[2] = 2;
        dp[3] = 4;

        for(int i = 4; i <= n; i++) {
            dp[i] = ((dp[i - 1] + dp[i - 2]) % (int)(1e9 + 7) + dp[i - 3]) % (int)(1e9 + 7);
        }

        return dp[n];
    }
    /**
     * 1137. 第 N 个泰波那契数
     * https://leetcode.cn/problems/n-th-tribonacci-number/description/
     * @param n
     * @return
     */
    public int tribonacci(int n) {
        int[] dp = new int[n + 1];
        if(n == 0) {
            return 0;
        }
        if(n == 1 || n == 2) {
            return 1;
        }
        int a = 0;
        int b = 1;
        int c = 1;
        int d = 0;
        for(int i = 3; i <= n; i++) {
            d = a + b + c;
            a = b;
            b = c;
            c = d;
        }
        return d;
    }
}